import { Injectable, EventEmitter } from '@angular/core';

import { ConfigmanageApirequest } from '../ApiRequest/ConfigManage.Apirequest';
import { configmanageServiceInterface } from './interface/configmanage.service.interface';
@Injectable()
export class ConfigmanageService implements configmanageServiceInterface {

  /**
   * 获取指定应用单个配置
   * @param key 
   * @param appId 
   */
  async getAppConfigItem(key: string, appId: string): Promise<AppConfig> {
    return this.configmanageApirequest.getAppConfigItem(key, appId);
  }

  /**
   * 获取指定应用多个配置
   * @param keys 
   * @param appId 
   */
  async getAppConfigItems(keys: string[], appId: string): Promise<AppConfig[]> {
    return this.configmanageApirequest.getAppConfigItems(keys, appId);
  }

  /**
   * 获取指定应用所有配置
   * @param appId 
   */
  async getAppConfigAllItems(appId: string): Promise<AppConfig[]> {
    let resp = await this.configmanageApirequest.getAppConfigAllItems(appId);
    resp.forEach(config => {
      config.intercept = config.Value;
      config.Value = JSON.parse(config.Value);
      config.expand = false;
    });
    return resp;
  }

  /**
   * 创建指定app下的配置
   * @param Key 
   * @param Value 
   * @param AppId 
   */
  async createConfig(Key: string, Value: string, AppId: string): Promise<string> {
    return this.configmanageApirequest.createConfig(Key, Value, AppId);
  }

  /**
   * 删除指定app下的指定配置
   * @param Key 
   * @param AppId 
   */
  async deleteConfig(Key: string, AppId: string): Promise<any> {
    return this.configmanageApirequest.deleteConfig(Key, AppId);
  }

  /**
   * 更新指定app下的指定配置
   * @param Key 
   * @param Value 
   * @param AppId 
   */
  async updateConfig(Key: string, Value: string, AppId: string): Promise<any> {
    return this.configmanageApirequest.updateConfig(Key, Value, AppId);
  }

  constructor(
    private configmanageApirequest: ConfigmanageApirequest
  ) { }

}

export class AppConfig {
  "Key": string;
  "Value": string;
  expand: boolean;
  intercept: string;
}